/**
 * create by young (young2f@outlook.com) on 2020/12/4
 */
'use strict';
const base = require('../base/serviceBase');
const _ = require('lodash');

module.exports = class extends base {
  async create(body) {
    const { ctx } = this;
    const { key, tenantCode, table ,tabsCode} = body;
    console.log("create dlabel service",body);
    const where = { key, tenantCode, table,tabsCode };
    const res = await ctx.model.DLabel.findOne(where);
    if (res) {
      throw new Error(`key：${key} 已存在！`);
    }
    const result = await ctx.model.DLabel(body)
      .save();
    return { status: 'ok', result };
  }
  async getDLabelTableColumns(body) {
    const { ctx } = this;
    const {  tenantCode, table } = body;
    const db = await ctx.model.DLabel.find({ tenantCode,table });
    console.log('xxx',db)
    const result = [];
    _.forEach(db, item => {
      const { enableArea, title, key, inputType, type, selectOption } = item;
      if (_.includes(enableArea, 'table')) {
        const json = { title, key, dataIndex: key, inputType: inputType, selectOption, type };
        result.push(json);
      }
    });
    console.log(result);

    return { status: 'ok', result };
  }
};
